﻿using Orleans.Configuration;
using Orleans.Grains;
using Orleans.Hosting;
using System;
using System.Net;
using System.Threading.Tasks;

namespace Orleans.Server
{


    /// <summary>
    /// Silo 服务器
    /// </summary>
    public class Program
    {
        private static string Username = "admin";
        private static string Password = "123";
        private static int Port = 8080;

        public static async Task Main(string[] args)
        {
            var host = await StartServer();
            Console.WriteLine($"服务启动成功!\n");
            Console.WriteLine($"服务器面板UI: http://localhost:{Port}");
            Console.WriteLine($"用户名: {Username}");
            Console.WriteLine($"密码: {Password}");
            Console.ReadLine();
            await host.StopAsync();//停止当前Silo
        }

        /// <summary>
        ///  启动服务
        /// </summary>
        /// <returns></returns>
        private static async Task<ISiloHost> StartServer()
        {
            SiloHostBuilder builder = new SiloHostBuilder();

            //配置Silo只使用开发集群，并监听本地主机。 
            builder.UseLocalhostClustering();

            //获取或设置此服务的唯一标识符，该标识符应在部署和重新部署后仍然有效
            builder.Configure<ClusterOptions>(options =>
            {
                options.ClusterId = "dev";
                options.ServiceId = "MyHost";
            });

            //配置Silo的端口
            builder.Configure<EndpointOptions>(options => options.AdvertisedIPAddress = IPAddress.Loopback);

            //增加持久层
            builder.AddMemoryGrainStorage("DevStore");

            //加载Grains
            builder.ConfigureApplicationParts(parts =>
                parts.AddApplicationPart(typeof(EventGrain).Assembly).WithReferences());

 

            //增加仪表盘
            builder.UseDashboard(options =>
                {
                    options.Username = Username;
                    options.Password = Password;
                    options.Host = "*";
                    options.Port = Port;
                    options.HostSelf = true;
                    options.CounterUpdateIntervalMs = 1000;
                });

            ISiloHost host = builder.Build();
            await host.StartAsync();
            return host;
        }
    }
}
